package com.yfp.client.controller;

import cn.hutool.core.util.BooleanUtil;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.yfp.client.domain.MallProduct;
import com.yfp.client.domain.dto.MallOrderAddressDTO;
import com.yfp.client.domain.dto.MallOrderListReqDTO;
import com.yfp.client.domain.dto.PackSettleDTO;
import com.yfp.client.domain.vo.MallOrderListReqVO;
import com.yfp.client.service.MallOrderService;
import com.yfp.client.service.MallProductService;
import com.yfp.client.util.Response;
import com.yfp.common.core.domain.LogisticsInfoVO;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 商城商品
 *
 * @CreateDate: 2025/2/24 15:23
 * @UpdateUser:
 * @UpdateDate: 2025/2/24 15:23
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/client/mallProduct")
@Tag(name = "商城商品管理")
public class MallProductController extends BaseController {

    @Resource
    private MallProductService mallProductService;
    @Resource
    private MallOrderService mallOrderService;

    @GetMapping("/list")
    @Operation(summary = "商品列表")
    public Response<TableDataInfo<List<MallProduct>>> getList() {
        startPage();
        return Response.success(getDataTable(mallProductService.getList(null)));
    }

    @GetMapping("/{id}")
    @Operation(summary = "详情")
    R<MallProduct> info(@PathVariable Long id) {
        MallProduct mallProduct = mallProductService.findInfoById(id);
        return R.ok(mallProduct);
    }

    @PostMapping("/settle")
    @Operation(summary = "套餐结算-创建订单", description = "小程序创建订单使用-订单结算-确认支付")
    public Response<Long> settle(@RequestBody PackSettleDTO packSettleDTO) {
        Long mallOrderId = mallOrderService.settle(packSettleDTO);
        return Response.success(mallOrderId);
    }

    @GetMapping("/settle/check")
    @Operation(summary = "套餐结算-去支付前校验", description = "小程序创建订单使用-订单结算-去支付前校验")
    public Response<Integer> settleCheck(@Schema(description = "订单id") @Parameter Long mallOrderId) {
        Boolean canBuy = mallOrderService.settleOrderCheck(mallOrderId);
        Integer canBuyInteger = BooleanUtil.toInteger(canBuy);
        return Response.success(canBuyInteger);
    }

    @GetMapping("/settle/payment")
    @Operation(summary = "套餐结算-正式付款,微信、微信+积分", description = "小程序创建订单使用-套餐结算-正式付款-微信+积分")
    public R<WxPayUnifiedOrderV3Result.JsapiResult> rechargeSettlePayment(@Schema(description = "订单id") @Parameter Long mallOrderId) {
        WxPayUnifiedOrderV3Result.JsapiResult jsapiResult = mallOrderService.rechargeSettlePayment(mallOrderId);
        return R.ok(jsapiResult);
    }

    @GetMapping("/settle/payment/integral")
    @Operation(summary = "套餐结算-正式付款,积分", description = "小程序创建订单使用-套餐结算-正式付款-积分")
    public R<Void> rechargeSettlePaymentIntegral(@Schema(description = "订单id") @Parameter Long mallOrderId) {
        mallOrderService.rechargeSettlePaymentIntegral(mallOrderId);
        return R.ok();
    }

    @GetMapping("/cancel")
    @Operation(summary = "取消订单", description = "小程序取消订单使用")
    public Response<Boolean> cancelOrder(@RequestParam Long mallOrderId) {
        return Response.success(mallOrderService.cancelOrder(mallOrderId));
    }

    @GetMapping("/confirm")
    @Operation(summary = "确认收货", description = "小程序确认收货订单使用")
    public Response<Boolean> confirmOrder(@RequestParam Long mallOrderId) {
        return Response.success(mallOrderService.confirmOrder(mallOrderId));
    }

    @PutMapping("/order/address")
    @Operation(summary = "修改订单收货地址", description = "小程序订单收货地址使用")
    public Response<Void> updateAddress(@RequestBody MallOrderAddressDTO mallOrderAddressDTO) {
        mallOrderService.updateAddress(mallOrderAddressDTO);
        return Response.success();
    }

    @GetMapping("/order/list")
    @Operation(summary = "订单列表", description = "小程序订单列表页面使用")
    public Response<TableDataInfo<MallOrderListReqVO>> getClientOrderList(MallOrderListReqDTO mallOrderListReqDTO) {
        TableDataInfo<MallOrderListReqVO> clientOrderList = mallOrderService.getClientOrderList(mallOrderListReqDTO);
        return Response.success(clientOrderList);
    }

    @GetMapping("/order/info")
    @Operation(summary = "订单详情", description = "小程序订单详情页面使用")
    public Response<MallOrderListReqVO> getClientOrderInfo(@RequestParam Long mallOrderId) {
        MallOrderListReqVO clientOrderList = mallOrderService.getClientOrderInfo(mallOrderId);
        return Response.success(clientOrderList);
    }

    @GetMapping("/order/logistics")
    @Operation(summary = "查询订单物流信息", description = "小程序查询订单物流信息使用")
    public Response<LogisticsInfoVO> getOrderLogisticsInfo(@RequestParam Long mallOrderId) {
        LogisticsInfoVO logisticsInfo = mallOrderService.getOrderLogisticsInfo(mallOrderId);
        return Response.success(logisticsInfo);
    }
}
